﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public partial class frmThukho : Form
    {
        public frmThukho()
        {
            InitializeComponent();
            addColumn1();
            addColumn2();
            addColumn3();
        }

        private void addColumn1()
        {
            DataGridViewTextBoxColumn column1, column2, column3, column4;
            column1 = new DataGridViewTextBoxColumn();
            column1.HeaderText = "Id";
            column1.Width = 50;

            column2 = new DataGridViewTextBoxColumn();
            column2.HeaderText = "Ma phiếu";
            column2.Width = 100;

            column3 = new DataGridViewTextBoxColumn();
            column3.HeaderText = "Ngày nhập";
            column3.Width = 100;

            column4 = new DataGridViewTextBoxColumn();
            column4.HeaderText = "Ngày xuất";
            column4.Width = 100;

            dataGridView1.Columns.AddRange(new DataGridViewColumn[] { column1, column2, column3, column4 });
            dataGridView1.RowHeadersVisible = true;
        }
        private void addColumn2()
        {
            DataGridViewTextBoxColumn column1, column2, column3, column4, column5;
            column1 = new DataGridViewTextBoxColumn();
            column1.HeaderText = "STT";
            column1.Width = 50;

            column2 = new DataGridViewTextBoxColumn();
            column2.HeaderText = "Mã mặt hàng";
            column2.Width = 100;

            column3 = new DataGridViewTextBoxColumn();
            column3.HeaderText = "Tên mặt hàng";
            column3.Width = 100;

            column4 = new DataGridViewTextBoxColumn();
            column4.HeaderText = "Số lượng";
            column4.Width = 100;

            column5 = new DataGridViewTextBoxColumn();
            column5.HeaderText = "Ghi chú";
            column5.Width = 100;

            dataGridView2.Columns.AddRange(new DataGridViewColumn[] { column1, column2, column3, column4, column5 });
            dataGridView2.RowHeadersVisible = true;
        }
        private void addColumn3()
        {
            DataGridViewTextBoxColumn column1, column2, column3, column4, column5;
            column1 = new DataGridViewTextBoxColumn();
            column1.HeaderText = "STT";
            column1.Width = 50;

            column2 = new DataGridViewTextBoxColumn();
            column2.HeaderText = "Mã mặt hàng";
            column2.Width = 100;

            column3 = new DataGridViewTextBoxColumn();
            column3.HeaderText = "Tên mặt hàng";
            column3.Width = 100;

            column4 = new DataGridViewTextBoxColumn();
            column4.HeaderText = "Số lượng";
            column4.Width = 100;

            column5 = new DataGridViewTextBoxColumn();
            column5.HeaderText = "Ghi chú";
            column5.Width = 100;

            dataGridView3.Columns.AddRange(new DataGridViewColumn[] { column1, column2, column3, column4, column5 });
            dataGridView3.RowHeadersVisible = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtID.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtMaphieu.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtNgaynhap.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtNgayxuat.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(txtID.Text, txtMaphieu.Text, txtNgaynhap.Text, txtNgayxuat.Text);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int icountSelcetRow = dataGridView1.SelectedRows.Count;
            if (icountSelcetRow == 0)
            {
                MessageBox.Show("ban chua chon dong nao ca");
            }
            else if (icountSelcetRow == 1)
            {
                dataGridView1.CurrentRow.Cells[1].Value = txtID.Text;
                dataGridView1.CurrentRow.Cells[2].Value = txtMaphieu.Text;
                dataGridView1.CurrentRow.Cells[3].Value = txtNgaynhap.Text;
                dataGridView1.CurrentRow.Cells[4].Value = txtNgayxuat.Text;
            }
            else
            {
                MessageBox.Show("bạn chỉ được chọn 1 dòng");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int icountSelcetRow = dataGridView1.SelectedRows.Count;
            if (icountSelcetRow == 0)
            {
                MessageBox.Show("bạn hay chon dòng cần xóa");
            }
            else 
            {
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    if (!row.IsNewRow) dataGridView1.Rows.Remove(row);
            }   
        }
    }
}
